package teaching.io.entity;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 学生类。
 *
 * @author deng.zhao
 * @version 1.0 2018/04/03
 * 
 */
public class Student implements Externalizable {
	// 学号；
	private String id;
	// 姓名；
	private String name;
	// 出生日期；
	private Date birthDate;
	// 性别；
	private Character gender;

	/**
	 * 无参构造方法。
	 */
	public Student() {
		super();
	}

	/**
	 * 有参构造函数。
	 *
	 * @param id
	 *            编号；
	 * @param name
	 *            姓名；
	 * @param birthDate
	 *            出生日期；
	 * @param gender
	 *            性别；
	 */
	public Student(String id, String name, Date birthDate, Character gender) {
		super();
		this.id = id;
		this.name = name;
		this.birthDate = birthDate;
		this.gender = gender;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public Character getGender() {
		return gender;
	}

	public void setGender(Character gender) {
		this.gender = gender;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", " + "birthDate=" + birthDate + ", gender=" + gender + "]";
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeUTF(id);
		out.writeUTF(name);
		final GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(birthDate);
		out.writeInt(calendar.get(Calendar.YEAR));
		out.writeInt(calendar.get(Calendar.MONTH));
		out.writeInt(calendar.get(Calendar.DATE));
		out.writeChar(gender);
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		id = in.readUTF();
		name = in.readUTF();
		final GregorianCalendar calender = new GregorianCalendar(in.readInt(), in.readInt(), in.readInt());
		birthDate = calender.getTime();
		gender = in.readChar();
	}

}
